package com.jt.blog.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.blog.dao.TagMapper;
import com.jt.blog.domain.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@Component
public class TagServiceImpl implements TagService{
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Cacheable(value = "tagCache")
    @Override
    public List<Tag> selectTags() {
       return tagMapper.selectList(null);

    }

    /**
     *
     * @param tag
     *  @CacheEvict注解的作用是定义切入点方法，执行此注解描述的方法时，
     *  底层通过AOP放的执行redis数据的清楚操作
     */
//    @CacheEvict(value = "tagCache",
//            allEntries = true,//删除key对应的所有数据
//            beforeInvocation = false)//执行insert之后，删除
    @CachePut(value = "tagCache")
    @Override
    public void insertTag(Tag tag){
//        tagMapper.insert(tag);
//        ValueOperations forValue = stringRedisTemplate.opsForValue();
//        List<Tag> cache = (List<Tag>) forValue.get("tagCache");
//        cache.add(tag);
//        forValue.set("tagCache",cache )
        tagMapper.insert(tag);
    }
}
